<template>
  <div>
    <AreaTree ref="treeRef" :key="reload"  api="admin_sysadministration_tree" v-bind="$attrs" @change="handleOperate">
    </AreaTree>
    <el-dialog v-model="visible" :title="dialogTitle">
      <el-space wrap fill>
        <AplidForm v-if="visible" v-bind="areaEditFormConfig(formModel)" v-model="formModel" @submit="onSubmit">
        </AplidForm>
      </el-space>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import AreaTree from './AreaTree/index.vue'
import { areaEditFormConfig } from '../config'
import {ElMessage} from 'element-plus'
const reload = ref()
const emit = defineEmits(['node-click', 'refresh'])
const visible = ref(false)
const dialogTitle = ref('操作')
const formModel = ref({
  administrationPid: '',
  administrationId: '',
})


const onSubmit = (form, done) => {
  apiMod
    .sysadministrationapprover_update({
      administrationId: form.administrationId,
      approverIds: form.approverIds,
    })
    .then((res) => {
      ElMessage.success('操作成功!')
      visible.value = false
    })
  done()
}
const handleOperate = (type, data, node) => {
  if (type == 'edit') {
    apiMod.sysadministrationapprover_getList(data.administrationId).then((res: any) => {
      visible.value = true
      formModel.value = {
        ...data || {},
        approverIds: res.data?.map((item) => item.userId).join(','),
        administrationNameTag: data.administrationName + '民政局',
      }
    })
  }
  emit('node-click', data)
}
</script>

<style scoped></style>
